#
# $Header: mkinitramfs-ll/hooks/zram                     Exp $
# $Author: (c) 2011-6 tokiclover <tokiclover@gmail.com>  Exp $
# $License: 2-clause/new/simplified BSD                  Exp $
# $Version: 0.22.1 2019/01/02 12:33:03                   Exp $
#

[ -z "$zram" ] && get_cmdline_option "zram"

enable_zram() {
	echo $$ >/run/${0##*/}.pid
	eval set -- $(echo "${zram%%:*}" | sed -e 's/-/ /g')

	local compressor="$3" devices="${1:-4}" streams="${2:-2}"
	debug -d modprobe zram num_devices=$devices
	case "$compressor" in
		(lz4|lzo)        :;;
		(*) compressor=lz4;;
	esac
	[ -w /sys/block/zram0/comp_algorithm ] || compressor=
	[ -w /sys/block/zram0/max_comp_streams ] || streams=

	local name=zram num=0 dev device fs mopt size
	for device in $(echo "${zram#*:}" | sed -e 's/:/ /g'); do
		[ "$num" = "$num_devices" ] && break

		eval set -- $(echo "$device" | sed -e 's/-/ /g')
		size="$1" fs="$2" dev=/sys/block/zram$num
		[ -n "$size" ] || continue
		[ -b /dev/zram$num ] || break

		[ -n "$compressor" ] && echo $compressor >$dev/comp_algorithm
		[ -n "$streams"    ] && echo $streams    >$dev/max_comp_streams
		                        echo $size       >$dev/disksize
		num=$(($num + 1))
		[ -n "$fs" ] || continue
		if [ -x /sbin/mkfs.$fs ]; then
			debug -d /sbin/mkfs.$fs /dev/${dev##*/}
		else
			error "mkfs.$fs not found"
		fi
	done
	rm -f /run/${0##*/}.pid
}

retval=0

if [ -n "$zram" ]; then
	enable_zram
	retval="$?"
fi

exit "$retval"

#
# vim:fenc=utf-8:ft=sh:ci:pi:sts=0:sw=4:ts=4:
#
